<?php
/**
 * 用户管理
 * @copyright (c) Seer All Rights Reserved
 */

require_once 'globals.php';

$Card_Model = new Card_Model();
$action = $_POST['action'];
$mode = $_POST['mode'];
$cardName = isset($_REQUEST['cardName']) ? $_REQUEST['cardName'] : '';
$page = $_GET['pid'] ? $_GET['pid'] : '1';
$pic_type = array('gif', 'jpg', 'jpeg','png');
//显示学生(学生一览表检索)
if ($action == '' ) {

	$sqlSegment .= ' ORDER BY buildtime DESC ';
	$pwd = '优惠卡管理';
	$addUrl = $cardName ? "cardName={$cardName}&" : '';
	$cardsNum = $Card_Model->getCardsNum($cardName);
	$cards = $Card_Model->getCardsForAdmin($sqlSegment, $page, $cardName);
	$pageurl =  pagination($cardsNum, Option::get('admin_perpage_num'), $page, "card.php?{$addUrl}pid=");
	include View::getView('header');
	require_once View::getView('card/card_list');
	include View::getView('footer');View::output();
}

// 优惠卡删除
if ($action == 'delete' ) {
	$cards = isset($_POST['ids']) ? $_POST['ids'] : array();

		$Card_Model->delCards($cards);

	 seerDirect("./card.php?active_del=1&cardName={$cardName}&pid={$page}");
}
// 优惠卡信息编辑
if ($action == 'update') {;
	$ID = $_POST['ID'];

	// 编辑画面初期化
	if(!$mode)
	{
		$card=$Card_Model->getOneCardForUpdate($ID);
		include View::getView('header');
		require_once View::getView('card/card_update');
		include View::getView('footer');
		View::output();
	}
	// 编辑信息提交
	else if($mode=="commit")
	{
		$pre_card_cover=$_POST['pre_card_cover'];

		$card_name = $_POST['card_name'];
		$card_deadline = $_POST['card_deadline']?$_POST['card_deadline']:"2099-12-29";
		$card_comment = $_POST['card_comment'];

		if ($_FILES['card_cover']['size'] > 0) {
			$imgSize=getimagesize($_FILES['card_cover']['tmp_name']);
			if($imgSize[0]!=450 || $imgSize[1]!=230)
			{
				emMsg("图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：450px*230px");
				exit;
			}
			$file_info = uploadFile($_FILES['card_cover']['name'], $_FILES['card_cover']['error'], $_FILES['card_cover']['tmp_name'], $_FILES['card_cover']['size'], $pic_type, false);
			if (!empty($file_info['file_path'])) {
				$card_cover = $file_info['file_path'];
			}
			if(file_exists($pre_card_cover))
			{
				unlink($pre_card_cover);
			}
		}
		else
		{
			$card_cover	=$pre_card_cover;
		}

		$ID = $_POST['ID'];

		$card = array(
			'ID'=>$ID,
			'card_name' => $card_name,
			'card_deadline' => $card_deadline,
			'card_cover' => $card_cover,
			'card_comment' => $card_comment,
		);
		var_dump($card);
		$Card_Model->editCard($card);
		seerDirect("./card.php?active_update=1&cardName={$card_name}");
	}
}
// 优惠卡添加
if ($action == 'add') {
	// 添加优惠卡画面初期化
	if(!$mode)
	{
		include View::getView('header');
		require_once View::getView('card/card_update');
		include View::getView('footer');
		View::output();
	}
	// 添加优惠卡信息提交
	else if($mode=="commit")
	{
		if ($_FILES['card_cover']['size'] > 0) {
			$imgSize=getimagesize($_FILES['card_cover']['tmp_name']);
			if($imgSize[0]!=450 || $imgSize[1]!=230)
			{
				emMsg("图片尺寸错误。上传图片的尺寸：".$imgSize[0]."px*".$imgSize[1]."px，正确的尺寸：450px*230px");
				exit;
			}
			$file_info = uploadFile($_FILES['card_cover']['name'], $_FILES['card_cover']['error'], $_FILES['card_cover']['tmp_name'], $_FILES['card_cover']['size'], $pic_type, true);
			if (!empty($file_info['file_path'])) {
				$card_cover = !empty($file_info['thum_file']) ? $file_info['file_path'] : $file_info['file_path'];
				$card_cover="http://".$_SERVER['HTTP_HOST']."/m/".$card_cover;
			}
		}
		$card_name = $_POST['card_name'];
		$card_deadline = $_POST['card_deadline']?$_POST['card_deadline']:"2099-12-29";
		$card_comment = $_POST['card_comment'];
		$card = array(
			'card_name' => $card_name,
			'card_deadline' => $card_deadline,
			'card_cover' => $card_cover,
			'card_comment' => $card_comment,
		);
		 $Card_Model->addCard($card);
		seerDirect("./card.php?active_add=1&cardName={$card_name}");
	}
}